package com.example.webapp.controler;

import com.example.webapp.bean.User;
import com.example.webapp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class RegisterControler {
    @Autowired
    private UserService userService;

    @CrossOrigin
    @PostMapping("/register")
    @ResponseBody
    public String register(@RequestBody Map map){
        String usr=(String)map.get("username");
        String pwd=(String)map.get("password");
        User res=userService.selectUsername(usr);
        if(res!=null){
            return "dupuser";
        }else{
            User newuser=new User(usr,pwd);
            userService.insertUser(newuser);
            return "successinsert";
        }
    }
}
